<?php

require_once( 'mvc-exception.php' );

class MVC_Dispatcher
{
	public $request_factory = null;
	public $controller_factory = null;
	public $view_factory = null;

	public function __construct( $request_factory = null, $controller_factory = null, $view_factory = null )
	{
		$this->request_factory = $request_factory;
		$this->controller_factory = $controller_factory;
		$this->view_factory = $view_factory;
	}

	public function process()
	{
		$request = $this->request_factory->get_request();

		if ( $request == null )
		{
			throw new MVC_Exception( "Request factory returned a null request" );
		}

		$controller = $this->controller_factory->get_controller( $request );

		if ( $controller == null )
		{
			throw new MVC_Exception( "Controller factory returned a null controller" );
		}
		
		$response = $controller->respond( $request );

		if ( $response == null )
		{
			throw new MVC_Exception( "Controller returned a null response" );
		}

		$view = $this->view_factory->get_view( $response );

		if ( $view == null )
		{
			throw new MVC_Exception( "View factory returned a null view" );
		}
		
		$view->render( $response );
	}
}

?>
